package com.game.server.message.proto.execute;

import java.util.ArrayList;
import java.util.HashMap;

import com.comm.ISession;
import com.game.majiang.algorithm.chexuan.GamePlayerInfo;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.majiang.logic.chexuan.ChexuanTabale;
import com.game.server.message.proto.in.AbstractRealTimeRecordReq;
import com.game.server.message.proto.out.RealTimeRecordRsp;
import com.game.server.message.proto.out.RealTimeRecordStructrue;
import com.game.server.system.AbstractTable;

public class RealTimeRecordReq extends AbstractRealTimeRecordReq {

    @Override
    public void Execute(ISession session) {
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            AbstractTable abTable = client.getTable();
            if(abTable != null) {
            	ChexuanTabale table = (ChexuanTabale) abTable;
            	HashMap<Integer, GamePlayerInfo> mapRecord = table.getMapRecord();
            	int count = 0;
            	RealTimeRecordRsp rsp = new RealTimeRecordRsp();
            	rsp.setType(0);
            	ArrayList<RealTimeRecordStructrue> listRTRS = new ArrayList<RealTimeRecordStructrue>();
            	int i = 0;
            	for (Integer uid : mapRecord.keySet()) {
            		if(count == 30) {
						if(i > 30) {
							rsp.setType(1);
						}
						rsp.setRtrs(listRTRS.toArray(new RealTimeRecordStructrue[listRTRS.size()]));
						client.Send(rsp);
						listRTRS.clear();
						count = 0;
					}
            		GamePlayerInfo info = mapRecord.get(uid);
            		RealTimeRecordStructrue rtrs = new RealTimeRecordStructrue();
            		rtrs.setName(info.getName());
            		rtrs.setTotalBobo(info.getTotalBobo());
            		rtrs.setTotalWin(info.getUseBobo() - info.getTotalBobo());
            		rtrs.setUid(uid);
					listRTRS.add(rtrs);
					count++;
					i++;
				}
            	rsp.setType(2);
            	rsp.setRtrs(listRTRS.toArray(new RealTimeRecordStructrue[listRTRS.size()]));
            	client.Send(rsp);
            }
        }
    }

}
